/*
 * File:   main.cpp
 * Author: Kreyl
 *
 * Created on May 27, 2011, 6:37 PM
 */

#include "stm32f10x.h"

#include "main.h"
#include "kl_lib.h"
#include "led.h"
#include "flashloader_support.h"
#include "Iwire.h"


#define SIZE  100

// Prototypes
void GeneralInit(void);

bool KWP = false;
Led_t PowerLed, BlueLed;

// ============================ Implementation ================================
int main(void) {
    GeneralInit();

    PowerLed.On();

    uint32_t Tmr;


    // ==== Main cycle ====
    while(1) {
//        if(Delay.Elapsed(&Snd, 999)) {
//            Uart.Printf("%A\r", IWire.PRXBuf, *IWire.PIndex);
//            Uart.Printf("a");
//        }
        if(Delay.Elapsed(&Tmr, 50)){
            if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) && !KWP){
                PowerLed.Toggle();
//                TempSensor.WriteAndWaitPRNC();
               if(IWire.PRXBuf[*IWire.PIndex] == 0xCD){
                   IWire.Cmd(CMD_SET_DATA_MODE);
                   IWire.Cmd(CMD_READ_ROM);
               }// if present
               else if(IWire.PRXBuf[*IWire.PIndex] == 0x33){
                   IWire.Cmd(0xFF);
               }
               else {
                   IWire.Cmd(CMD_RESET_FLEX);
               } // if not present
//                JumpToFlashLdr();
                KWP = true;
            }
            if (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) && KWP){
                KWP = false;
            }
        }
        Uart.Task();
//        IWire.Task();
    } // while(1)
}

void GeneralInit(void) {
    // Init delay
    Delay.Init();
    PowerLed.Init(GPIOC, 9);  // Led
    BlueLed.Init(GPIOC, 8); // BlueLed
    klPinSetup(GPIOA, 0, pmInFloating, ps50MHz); // Button

    Uart.Init(9600);
    IWire.Init();
    Uart.Printf("\rUrelesta v.%u\r", CURRENT_FIRMWARE_VERSION);
}

void CmdUnit_t::NewCmdHandler(){
    if(Uart.RXBuf[0] == '0'){
        Uart.Printf("Reset: ");
        IWire.Cmd(CMD_RESET); // Reset
    }
    else if(Uart.RXBuf[0] == '1') {
        Uart.Printf("Convert T: ");
        IWire.Cmd(CMD_CONVERT_T); // Convert T
    }
    else if(Uart.RXBuf[0] == '2'){
        Uart.Printf("Read Scratchpad: ");
        IWire.Cmd(CMD_READ_SCRATCHPAD); // Read scratchpad
    }
    else if(Uart.RXBuf[0] == '3'){
        Uart.Printf("Write: ");
        IWire.Cmd(CMD_WRITE_SCRATCHPAD); // Write scratchpad
    }
    else if(Uart.RXBuf[0] == '4'){
        Uart.Printf("Copy\r");
        IWire.Cmd(CMD_COPY_SCRATCHPAD); // Copy scrachpad
    }
    else if(Uart.RXBuf[0] == '5'){
        Uart.Printf("Recall: ");
        IWire.Cmd(CMD_RECALL_E2); // Recall
    }
    else if(Uart.RXBuf[0] == '6'){
        Uart.Printf("Power status: ");
        IWire.Cmd(CMD_READ_POWER_SUPPLY); // Power status
    }
    else if(Uart.RXBuf[0] == '7'){
        Uart.Printf("Match ROM: ");
        IWire.Cmd(CMD_MATCH_ROM); // Match ROM
    }
    else if(Uart.RXBuf[0] == '8'){
        Uart.Printf("Read ROM: ");
        IWire.Cmd(CMD_READ_ROM); // Read ROM
    }
    else if(Uart.RXBuf[0] == '9'){
        Uart.Printf("Search ROM: ");
        IWire.Cmd(CMD_SEARCH_ROM);
    }
    else Uart.Printf("Error\r");
//  Place here
}
